import type { RouteLocationNormalized } from 'vue-router'
import { emitter, Handler } from '../event'
import { currentRoute } from './current'


export * from './current'

const key = Symbol('ROUTE_CHANGE')

export const setRouteEmitter = (to: RouteLocationNormalized) => emitter.emit(key, to)

export const listenerRouteChange = (handler: (route: RouteLocationNormalized) => void, immediate = true): void => {
  emitter.on(key, handler as Handler)
  if (immediate && currentRoute.value) {
    handler(currentRoute.value)
  }
}

export const removeRouteListener = () => emitter.off(key)
